package com.poker.base;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;


public class MyRealm  extends AuthorizingRealm {
    /**
     * 接口被配置需要鉴权会触发本方法
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /**
     * 获取用户信息的时候会触发
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String userId = authenticationToken.getPrincipal().toString();
        System.out.println("当前登录的用户"+userId);
        AuthenticationInfo info = new SimpleAuthenticationInfo(userId,((UsernamePasswordToken)authenticationToken).getPassword(),this.getName());
        return info;
    }


    public static String getUserId() {
        Subject subject = SecurityUtils.getSubject();
        System.out.println("sessionId"+subject.getSession().getId().toString());
        System.out.println("当前用户是否登陆"+subject.isAuthenticated());
        Object userPrimaryKey = subject.getPrincipal();
//        if (userPrimaryKey == null) {
//            System.out.println("获取不到当前用户！！！");
//            return "xuyan";
//        }
        return userPrimaryKey.toString();
    }

}
